package com.maxsalad.uc.dao;

import com.maxsalad.common.dao.BaseDao;
import com.maxsalad.uc.pojo.TDishEntity;
import com.maxsalad.uc.pojo.TOrderDetailEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-16.
 */
@Repository
public class DishDao extends BaseDao<TDishEntity> {
    private void baseSearchParam(TDishEntity dishEntity, StringBuffer hql) {
        if (dishEntity == null) {
            return;
        }
        if (dishEntity.getId() != 0) {
            hql.append(" and t.id =:id");
        }
        //shopId
        if (dishEntity.getShopId() != 0) {
            hql.append(" and t.shopId =:shopId");
        }
        //店铺编码
        if (StringUtils.isNotBlank(dishEntity.getBarcode())) {
            hql.append(" and t.barcode =:barcode");
        }
        //店铺名称
        if (StringUtils.isNotEmpty(dishEntity.getDishName())) {
            hql.append(" and t.dishName like :dishName");
            dishEntity.setDishName("%" + dishEntity.getDishName() + "%");
        }
        //description
        if (StringUtils.isNotEmpty(dishEntity.getDescription())) {
            hql.append(" and t.description like :description");
            dishEntity.setDescription("%" + dishEntity.getDescription() + "%");
        }

    }

    /**
     * 查询所有平台信息
     *
     * @param dishEntity
     * @return
     * @throws Exception
     */
    public Map queryAllDish(TDishEntity dishEntity) throws Exception {
        StringBuffer hql = new StringBuffer("from TDishEntity t where 1=1 ");
        this.baseSearchParam(dishEntity, hql);
        hql.append(" order by t.createTime desc ");
        return this.pageQueryByHql(hql.toString(), dishEntity);
    }

    public List<TDishEntity> queryDishByIds(Long[] ids) throws Exception {
        Map paramMap = new HashMap();
        paramMap.put("ids", ids);
        paramMap.put("pageSize", ids.length);

        StringBuffer hql = new StringBuffer("from TDishEntity t where 1=1 ");
        hql.append(" and t.id in :ids");
        hql.append(" order by t.createTime asc ");
        return this.pageQuery(hql.toString(), paramMap, false);
    }

    public TDishEntity getDish(String posPalId) {
        Map paramMap = new HashMap();
        paramMap.put("pospalId", posPalId);
        String hql = "from TDishEntity t where 1=1 and t.pospalId=:pospalId";
        List<TDishEntity> list = this.findByHql(hql, paramMap);

        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}